<?php
class ImportCsvColl{
    private $data = array();
    private $csv_index = null;
    private $keys = array();
    public function __construct($csv_data) {
        if(!is_array($csv_data))
            $this->getCsvLinesFromString($csv_data);
        
        $this->csv_index = new ImportCsv();
        $this->csv_index->setData(array_shift($csv_data));
        
        $this->keys = array('numero'                =>  array(  'label'   =>  'Numéro',                     'bind_id'       =>  null,   'type'  =>  'integer'),
                            'socle'                 =>  array(  'label'   =>  'Socle',                      'bind_id'       =>  null,   'type'  =>  'string'),
                            'emplacement'           =>  array(  'label'   =>  'Emplacement',                'bind_id'       =>  null,   'type'  =>  'string'),
                            'date_mise_service'     =>  array(  'label'   =>  'Date de mise en service',    'bind_id'       =>  null,   'type'  =>  'date'),
                            'numero_serie'          =>  array(  'label'   =>  'Numéro de série',            'bind_id'       =>  null,   'type'  =>  'string'),
                            'numero_lecteur_carte'  =>  array(  'label'   =>  'Numéro du lecteur de carte', 'bind_id'       =>  null,   'type'  =>  'string'),
                            'marque_id'             =>  array(  'label'   =>  'Marque',                     'bind_id'       =>  null,   'type'  =>  'MlMarque'),
                            'jeux'                  =>  array(  'label'   =>  'Jeux',                       'bind_id'       =>  null,   'type'  =>  'string'),
                            'date_cessation'        =>  array(  'label'   =>  'Date de cessation',          'bind_id'       =>  null,   'type'  =>  'date'),
                            'destination_cessation' =>  array(  'label'   =>  'Destination cessation',      'bind_id'       =>  null,   'type'  =>  'string'),
                            'token'                 =>  array(  'label'   =>  'Token',                      'bind_id'       =>  null,   'type'  =>  'monnaie'),
                            'bill_acceptor'         =>  array(  'label'   =>  'Bill Acceptor',              'bind_id'       =>  null,   'type'  =>  'boolean'),
                            'tito'                  =>  array(  'label'   =>  'Tito',                       'bind_id'       =>  null,   'type'  =>  'boolean'),
                            'cashless'              =>  array(  'label'   =>  'Cashless',                   'bind_id'       =>  null,   'type'  =>  'boolean'),
                            'taux_de_redistribution'=>  array(  'label'   =>  'Taux de redistribution',     'bind_id'       =>  null,   'type'  =>  'float'),
                            'denomination'          =>  array(  'label'   =>  'Dénomination',               'bind_id'       =>  null,   'type'  =>  'MlDenomination'));
        
        foreach($csv_data AS $line){
            $obj = new ImportCsv();
            $obj->setData($line);
            $this->add($obj);
        }
    }
    public function bindWidth($key,$id){
        if(is_array($this->keys[$key])){
            $this->keys[$key]['bind_id'] = (int)$id;
            return true;
        }else{
            throw new Exception('Erreur ImportCsvColl::bindWidth() '.$key.' introuvable !');
        }
    }
    public function getRenderRequiredKeys(){
        $selects_form = '<table id="affecter_champs">';
        $csv_keys = $this->csv_index->getRaw();
        foreach($this->keys AS $key => $value){
            $tmp = new sfWidgetFormChoice(array('choices' => $csv_keys));
            $selects_form .= '<tr><th>'.$value['label'].'</th><td>'.$tmp->render('champs['.$key.']').'</td></tr>';
        }
        $selects_form .= '</table>';
        return $selects_form;
    }
    public function add(ImportCsv $obj){
        $this->data[] = $obj;
    }
    public function saveFromCsv(){
        $tosave = array();
        foreach($this->data AS $line){
            $mlmachine = new MlMachines();
            foreach($this->keys AS $key => $param){
                $bdd_value = $line->getValue((int)$param['bind_id'], $param['type']);
                if($bdd_value === false){
                    unset($mlmachine);
                    break;
                }
                $method = 'set'.sfInflector::camelize($key);
                $mlmachine->$method($bdd_value);
            }
            if(isset($mlmachine)){
                $tosave[] = $mlmachine;
                unset($mlmachine);
            }
        }
        $nb = 0;
        foreach($tosave AS $machine){
            if(MlMachinesTable::getInstance()->findByNumero($machine->getNumero())->count() < 1){
                $machine->save();
                $nb++;
            }            
        }
        return $nb;
    }
    private function getCsvLinesFromString(&$csv_data){
        $csv_data = explode("\n",$csv_data);
        if(count($csv_data) < 1)
            throw new Exception('Données CSV mal formattées ImportCsvColl::getCsvLinesFromString()');
    }
    public function __toString(){
        var_dump($this->csv_index);
        return '';
    }
}
?>
